package com.club.football.controller;

import com.club.football.common.enums.FeedBackStatusEnum;
import com.club.football.dto.FeedBackStarDto;
import com.club.football.dto.FeedbackEditDto;
import com.club.football.dto.FeedbackSaveDto;
import com.club.football.dto.PageDto;
import com.club.football.entity.Feedback;
import com.club.football.req.FeedbackPageReq;
import com.club.football.service.FeedbackService;
import com.club.football.utils.R;
import com.club.football.vo.FeedbackVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.club.football.common.CommonConstants.DEL_OK_MSG;

/**
 * 反馈Controller
 *
 * @author 1
 * @date 2023/12/28
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {
    @Resource
    private FeedbackService feedbackService;


    /**
     * 页面
     *
     * @param pageDto 页数到页
     * @return {@link R}
     */
    @GetMapping("/page")
    public R<PageDto<Feedback>> page(PageDto<Feedback> pageDto, FeedbackPageReq feedbackPageReq) {
        return feedbackService.feedbackPage(pageDto, feedbackPageReq);
    }

    /**
     * 保存
     *
     * @param saveDto 将数据保存到
     * @return {@link R}<{@link String}>
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody @Valid FeedbackSaveDto saveDto) {
        return feedbackService.feedbackAdd(saveDto);
    }

    /**
     * 编辑
     *
     * @param editDto 编辑数据至
     * @return {@link R}<{@link String}>
     */
    @PutMapping ("/edit")
    public R<String> edit(@RequestBody @Valid FeedbackEditDto editDto) {
        return feedbackService.feedbackEdit(editDto);
    }

    /**
     * 删除
     *
     * @param id ID
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/remove/{id}")
    public R<String> remove(@PathVariable Long id) {
        Feedback feedback = feedbackService.getById(id);
        if (!FeedBackStatusEnum.PENDING.getStatus().equals(feedback.getStatus())) {
            return R.failed("反馈已在处理中，不可删除");
        }
        feedbackService.removeById(id);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 信息
     *
     * @param feedBackStarDto 反馈星形dto
     * @return {@link R}<{@link Feedback}>
     */
    @GetMapping("/info")
    public R<FeedbackVo> info(@Valid FeedBackStarDto feedBackStarDto) {
        return feedbackService.feedbackInfo(feedBackStarDto);
    }

    /**
     * Set Star
     *
     * @param feedBackStarDto 反馈星形dto
     * @return {@link R}<{@link String}>
     */
    @PutMapping("/star")
    public R<String> setStar(@RequestBody @Valid FeedBackStarDto feedBackStarDto) {
        return feedbackService.setStar(feedBackStarDto);
    }
}
